package com.xlh.enums;

import com.google.common.collect.Lists;
import com.xlh.dto.course.ResourceDTO;
import com.xlh.dto.course.ResourceInfoDTO;
import com.xlh.enums.course.ChapterTypeEnum;
import com.xlh.enums.course.CodeEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 资源类型
 * <p>
 * Created by lx on 2019/1/28.
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
public enum ResourcesTypeEnum {

    THEORY_VIDEO(1, "理论课学习视频", true, "re-video.png") {
        @Override
        public ResourceInfoDTO getResourceInfoDTO(List<ResourceDTO> resourceDTOS) {
            return new ResourceInfoDTO(resourceDTOS, resourceDTOS);
        }

        @Override
        public String getName(CodeEnum codeEnum) {
            if (codeEnum != null && codeEnum == CodeEnum.COLLEGE_DEGREE) {
                return "任务引导学习视频";
            }
            return getMessage();
        }
    },
    COURSEWARE(2, "学习课件", true, "re-ppt.png") {
        @Override
        public ResourceInfoDTO getResourceInfoDTO(List<ResourceDTO> resourceDTOS) {
            if (resourceDTOS.size() == 1) return new ResourceInfoDTO(resourceDTOS, resourceDTOS);
            return getResourceData(resourceDTOS);
        }

        @Override
        public List<String> getConvertFormat() {
            return Lists.newArrayList(".pdf");
        }
    },
    PRACTICE(3, "随堂练习", false, "re-word.png"),
    PREPARATION_MATERIALS(4, "备课材料", false, "re-word.png") {
        @Override
        public ResourceInfoDTO getResourceInfoDTO(List<ResourceDTO> resourceDTOS) {
            return getResourceData(resourceDTOS);
        }

        @Override
        public List<String> getConvertFormat() {
            return Lists.newArrayList(FileTypeEnum.MARKDOWN.getExtension(), FileTypeEnum.HTML.getExtension());
        }
    },
    LEARNING_RESOURCES(5, "学习资源") {
        @Override
        public List<String> getDefaultResourceName(ChapterTypeEnum chapterTypeEnum) {
            List<String> theoryData = Lists.newArrayList(THEORY_VIDEO.message, COURSEWARE.message, PRACTICE.message,
                    PRACTICE_ANSWER.message, PREPARATION_MATERIALS.message, COURSE_MATERIAL.message);
            List<String> experimentData = Lists.newArrayList(LABORATORY_MANUAL.message, COURSE_MATERIAL.message);
            if (ChapterTypeEnum.isContainsTheory(chapterTypeEnum.getCode())
                    && ChapterTypeEnum.isContainsExperiment(chapterTypeEnum.getCode())) {
                theoryData.addAll(experimentData);
                return theoryData.stream().distinct().collect(Collectors.toList());
            }
            if (ChapterTypeEnum.isContainsTheory(chapterTypeEnum.getCode())) {
                return theoryData;
            }
            if (ChapterTypeEnum.isContainsExperiment(chapterTypeEnum.getCode())) {
                return experimentData;
            }
            return null;
        }

        @Override
        public ResourceInfoDTO getResourceInfoDTO(
                List<ResourceDTO> resourceDTOS, ChapterTypeEnum chapterTypeEnum, Long chapterId, CodeEnum codeEnum) {
            // 获取默认资源名称
            List<String> defaultResourceName = getDefaultResourceName(chapterTypeEnum);

            // 对默认资源进行增加实验课视频
            List<ResourceDTO> experimentVideoData = resourceDTOS.stream()
                    .filter(resourceDTO ->
                            Objects.equals(resourceDTO.getType(), this.getCode())
                                    && StringUtils.contains(resourceDTO.getName(), EXPERIMENT_VIDEO.message)
                    ).collect(Collectors.toList());
            List<String> names = experimentVideoData.stream()
                    .map(ResourceDTO::getName).collect(Collectors.toList());
            if (experimentVideoData.size() > 0) {
                defaultResourceName.addAll(names);
            }

            List<ResourceDTO> result = Lists.newArrayList();
            defaultResourceName.forEach(resourceName -> {

                // 过滤数据
                List<ResourceDTO> data = resourceDTOS.stream()
                        .filter(resourceDTO ->
                                Objects.equals(resourceDTO.getType(), this.getCode())
                                        && Objects.equals(resourceDTO.getName(), resourceName)
                        ).collect(Collectors.toList());

                // 将数据放到结果集中
                ResourceDTO resource = CollectionUtils.isEmpty(data) ? new ResourceDTO() : data.get(0);
                if (CollectionUtils.isEmpty(data)) {
                    resource.setType(this.getCode());
                    resource.setName(resourceName);
                }
                if (resource.getChapterId() == null) {
                    resource.setChapterId(chapterId);
                }
                // 如果是理论课学习视频 并且 还是 专科 需要改字
                if (codeEnum != null && codeEnum == CodeEnum.COLLEGE_DEGREE
                        && Objects.equals(resourceName, THEORY_VIDEO.message)) {
                    resource.setShowName(THEORY_VIDEO.getName(codeEnum));
                }
                resource.setAllowModify(names.contains(resourceName) ? false : true);

                result.add(resource);
            });
            List<ResourceDTO> resultData = setIconPath(result, this);

            return new ResourceInfoDTO(resultData, resultData);
        }
    },
    OTHER_RESOURCES(6, "其他资源", false, "re-zip1.png") {
        @Override
        public ResourceInfoDTO getResourceInfoDTO(List<ResourceDTO> resourceDTOS) {
            resourceDTOS = setIconPath(resourceDTOS, this);
            return new ResourceInfoDTO(resourceDTOS, resourceDTOS);
        }
    },
    PRESET_RESOURCE(7, "预置资源包"),
    LABORATORY_MANUAL(8, "实验手册", false, "re-wordOrZip.png") {
        @Override
        public ResourceInfoDTO getResourceInfoDTO(List<ResourceDTO> resourceDTOS) {
            return getResourceData(resourceDTOS);
        }

        @Override
        public List<String> getConvertFormat() {
            return Lists.newArrayList(FileTypeEnum.MARKDOWN.getExtension(), FileTypeEnum.HTML.getExtension());
        }
    },
    EXPERIMENT_VIDEO(9, "实验课学习视频", false, "re-video.png") {
        @Override
        public ResourceInfoDTO getResourceInfoDTO(List<ResourceDTO> resourceDTOS) {
            return new ResourceInfoDTO(resourceDTOS, resourceDTOS);
        }
    },
    TEMPLATE_WORD(10, "模板word"),
    TEMPLATE_ONLINE(11, "模板markdown"),
    PRACTICE_ANSWER(12, "随堂练习答案", false, "re-word.png"),
    COURSE_MATERIAL(13, "课程材料", false, "re-zip2.png"),
    LABORATORY_TARGET(14, "实验目标", false, "") {
        @Override
        public ResourceInfoDTO getResourceInfoDTO(List<ResourceDTO> resourceDTOS) {
            return getResourceData(resourceDTOS);
        }

        @Override
        public List<String> getConvertFormat() {
            return Lists.newArrayList(FileTypeEnum.MARKDOWN.getExtension(), FileTypeEnum.HTML.getExtension());
        }
    },
    COURSE_OUTLINE(15, "课程大纲") {
        @Override
        public ResourceInfoDTO getResourceInfoDTO(List<ResourceDTO> resourceDTOS) {
            return getResourceData(resourceDTOS);
        }

        @Override
        public List<String> getConvertFormat() {
            return Lists.newArrayList(FileTypeEnum.MARKDOWN.getExtension(), FileTypeEnum.HTML.getExtension());
        }
    },
    SKILL_INFO(16, "技能岗位"),
    EVALUATION(17, "评语模板");

    private Integer code;

    private String message;

    private Boolean resourceSynchronized = false;

    private String iconName;

    ResourcesTypeEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public String getName(CodeEnum codeEnum) {
        return getMessage();
    }

    public List<String> getConvertFormat() {
        return null;
    }

    public List<String> getDefaultResourceName(ChapterTypeEnum chapterTypeEnum) {
        return null;
    }

    public ResourceInfoDTO getResourceInfoDTO(
            List<ResourceDTO> resourceDTOS, ChapterTypeEnum chapterTypeEnum, Long chapterId, CodeEnum codeEnum) {
        return null;
    }

    public ResourceInfoDTO getResourceInfoDTO(List<ResourceDTO> resourceDTOS) {
        return null;
    }

    public static ResourcesTypeEnum getByCode(Integer code) {
        for (ResourcesTypeEnum resourcesTypeEnum : ResourcesTypeEnum.values()) {
            if (Objects.equals(resourcesTypeEnum.getCode(), code)) {
                return resourcesTypeEnum;
            }
        }
        return null;
    }

    public static ResourcesTypeEnum getByMessage(String message) {
        for (ResourcesTypeEnum resourcesTypeEnum : ResourcesTypeEnum.values()) {
            if (Objects.equals(resourcesTypeEnum.getMessage(), message)) {
                return resourcesTypeEnum;
            }
        }
        return null;
    }

    public static List<ResourcesTypeEnum> getByCodes(List<Integer> codes) {
        List<ResourcesTypeEnum> resourcesTypeEnums = Lists.newArrayList();
        codes.forEach(code -> {
            ResourcesTypeEnum typeEnum = getByCode(code);
            if (typeEnum != null) {
                resourcesTypeEnums.add(typeEnum);
            }
        });
        return resourcesTypeEnums;
    }

    public static String getMessageByCode(Integer code) {
        ResourcesTypeEnum resourcesTypeEnum = getByCode(code);
        return resourcesTypeEnum == null ? null : resourcesTypeEnum.getMessage();
    }

    public ResourceInfoDTO getResourceData(List<ResourceDTO> resourceDTOS) {
        if (CollectionUtils.isEmpty(resourceDTOS) || resourceDTOS.size() % 2 != 0) return new ResourceInfoDTO();

        List<ResourceDTO> creativeVersion = Lists.newArrayList();
        List<ResourceDTO> displayVersion = Lists.newArrayList();

        for (int i = 0; i < resourceDTOS.size(); i += 2) {
            creativeVersion.add(resourceDTOS.get(i));
            displayVersion.add(resourceDTOS.get(i + 1));
        }
        return new ResourceInfoDTO(creativeVersion, displayVersion);
    }

    public List<ResourceDTO> setIconPath(List<ResourceDTO> resourceDTOS, ResourcesTypeEnum resourcesTypeEnum) {
        // 设置图标
        resourceDTOS.forEach(resourceDTO -> {
            resourceDTO.setIconPath(LocalResourcesEnum.SYSTEM_FILE.getMappingUrl(resourcesTypeEnum.getIconName()));
            if (resourcesTypeEnum == ResourcesTypeEnum.LEARNING_RESOURCES) {
                List<String> iconNames = Arrays.stream(ResourcesTypeEnum.values())
                        .filter(typeEnum -> StringUtils.contains(resourceDTO.getName(), typeEnum.getMessage()))
                        .map(ResourcesTypeEnum::getIconName)
                        .collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(iconNames)) {
                    resourceDTO.setIconPath(
                            LocalResourcesEnum.SYSTEM_FILE.getMappingUrl(iconNames.get(0)));
                }
            }
        });

        return resourceDTOS;

    }

    /**
     * 学生端屏蔽的内容
     *
     * @return
     */
    public static List<String> getStudentShieldContent() {
        return Lists.newArrayList(ResourcesTypeEnum.PRACTICE_ANSWER.getMessage(),
                ResourcesTypeEnum.PREPARATION_MATERIALS.getMessage());
    }

    }
